<template>
	<view>
		<view class="box">
			<view style="padding-top: 30vh;" v-if="!resObj.troubleId">
				<u-empty mode="list"></u-empty>
			</view>
			<view v-if="resObj.troubleId">
				<assessCard :code="index" :resObj="resObj"></assessCard>
			</view>
			<view class="maintenance">
				<view v-for="(item,index) in iconList" :key="item.name" @click="clickMenus(item.path)">
					<image :src="item.iconSrc"></image>
					<view>{{item.name}}</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" mode="center" @close="show=false">
			<view class="popup">
				<view class="mb50">{{popTitle}}</view>
				<view class="flex flex-two">
					<view class="btn but1" @click="show=false">取消</view>
					<view class="btn but2" @click="sub">确定</view>
				</view>
			</view>
		</u-popup>
		<optionCard :show="showfsr" @showPopup="showfsr=false" @listItem="listItemfs" :event="true" :roleCode="roleCode"
			:selfFlag="selfFlag">
		</optionCard>
	</view>
</template>

<script>
	import optionCard from "./components/option-card.vue"
	import assessCard from "./components/assess-card.vue"
	const d = new Date()
	const year = d.getFullYear()
	let month = d.getMonth() + 1
	month = month < 10 ? `0${month}` : month
	let date = d.getDate()
	date = date < 10 ? `0${date}` : date
	export default {
		components: {
			assessCard,
			optionCard
		},
		data() {
			return {
				selfFlag: '',
				defaultDateMultiple: [`${year}-${month}-${date}`], //默认时间
				dictList: [],
				roleCode: '',
				showfsr: false, //选择申请人
				popTitle: '',
				show: false,
				//菜单icon展示下标
				iconList: [],
				//菜单所有icon
				list: [{
					name: '编辑',
					iconSrc: require('../../static/iconSave.png'),
					path: 'governInfoModification'
				}, {
					name: '发送',
					iconSrc: require('../../static/iconSend.png'),
					path: 'iconSend'
				}, {
					name: '审核',
					iconSrc: require('../../static/iconAudit.png'),
					path: 'audit?flag=0'
				}, {
					name: '非隐患',
					iconSrc: require('../../static/iconIssue.png'),
					path: 'Issue'
				}, {
					name: '退回',
					iconSrc: require('../../static/iconSendBack.png'),
					path: 'audit?flag=1&hidden=1'
				}, {
					name: '申请验收',
					iconSrc: require('../../static/iconApplyForAcceptance.png'),
					path: 'applyForAcceptance'
				}, {
					name: '验收',
					iconSrc: require('../../static/iconInspection.png'),
					path: 'Inspection'
				}, {
					name: '指派验收',
					iconSrc: require('../../static/iconAssignTheAcceptance.png'),
					path: 'assignTheAcceptance'
				}, {
					name: '审核信息',
					iconSrc: require('../../static/iconAuditInformation.png'),
					path: 'auditOpinion'
				}, {
					name: '流转信息',
					iconSrc: require('../../static/iconTransferInformation.png'),
					path: 'transferInformation'
				}, ],
				index: "0",
				resObj: {},
				popIndex: 0,
				params: {},
				currentState2: '',
				ysIndex: 0 //验收选人下标
			}
		},
		onLoad(e) {
			if (e.hiddenDangerId) {
				this.params.troubleId = e.hiddenDangerId
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			//初始化函数
			init() {
				console.log('刷新了')
				this.resObj = {}
				this.dictList = []
				this.iconList = []
				this.index = "0"
				this.requestList()
			},
			//展示下面的操作菜单
			showIcon() {
				if (this.resObj.buttonDisplay) {
					let arr = []
					arr = this.resObj.buttonDisplay.split(',')
					// console.log(arr, 5646546546)
					arr.forEach((item) => {
						let i = item * 1 - 1
						// console.log(i, this.list[i], this.list.length)
						this.iconList.push(this.list[i])
					})
				}
			},
			toJSON() {},
			//请求后对时间格式化，以及对部分数据做默认回显
			requestList() {
				let user = uni.getStorageSync('userInfo');
				this.request(this.$api.selectDataById, this.params, "POST").then((res) => {
					this.resObj = res.resultValue.items[0]
					this.resObj.caPeople = user.userName
					this.resObj.caOrganizeUnit = user.unitName
					this.resObj.caPeopleDate = this.defaultDateMultiple[0]
					this.resObj.caGroupDate = this.defaultDateMultiple[0]
					this.resObj.functDeptPeople = user.userName
					this.resObj.evaPeopleId = this.resObj.evaPeopleId == '' ? user.userId : this.resObj.evaPeopleId
					this.resObj.evaPeople = this.resObj.evaPeople == '' ? user.userName : this.resObj.evaPeople
					this.resObj.evaCoLeader = this.resObj.evaCoLeader == '' ? user.userName : this.resObj
						.evaCoLeader
					this.resObj.createDate = this.resObj.createDate ? this.resObj.createDate.slice(0, 10) : ''
					this.resObj.evaCoLeaderDate = this.resObj.evaCoLeaderDate ? this.resObj.evaCoLeaderDate.slice(
						0, 10) : this.defaultDateMultiple[0]
					this.resObj.evaPeopleDate = this.resObj.evaPeopleDate ? this.resObj.evaPeopleDate.slice(0,
						10) : this.defaultDateMultiple[0]
					this.resObj.findDate = this.resObj.findDate ? this.resObj.findDate.slice(0, 10) : ''
					this.resObj.functDeptPeopleDate = this.resObj.functDeptPeopleDate ? this.resObj
						.functDeptPeopleDate.slice(0, 10) : this.defaultDateMultiple[0]
					this.resObj.updateDate = this.resObj.updateDate ? this.resObj.updateDate.slice(0, 10) : ''
					this.resObj.evaGrade = this.resObj.evaGrade == '' ? this.resObj.zdGrade : this.resObj.evaGrade
					this.resObj.rnvStartDate = this.resObj.rnvStartDate ? this.resObj.rnvStartDate.slice(0, 10) :
						''
					this.resObj.rnvEndDate = this.resObj.rnvEndDate ? this.resObj.rnvEndDate.slice(0, 10) : ''
					this.index = this.resObj.currentState
					this.currentState2 = this.resObj.currentState2
					this.dictList = [] = res.resultValue.dicts
					// console.log(this.resObj, 666)
					this.dictsFn()
					this.showIcon()
				})
			},
			//根据数据字典key值回显
			dictsFn() {
				let obj = {
					specialtyAssort: this.resObj.specialtyAssort,
					troubleSource: this.resObj.troubleSource,
					attachFunctDept: this.resObj.attachFunctDept,
					priorEvaGrade: this.resObj.priorEvaGrade,
					evaGrade: this.resObj.evaGrade,
					zdGrade: this.resObj.zdGrade,
				}
				let arr = []
				for (let k in obj) {
					this.dictList.forEach(item => {
						if (k == item.name) {
							item.values.forEach(i => {
								if (obj[k] == i.value) {
									arr.push({
										name: k,
										value: i.text
									})
								}
							})
						}
					})
				}
				// console.log(this.dictList, 123456)
				arr.forEach((item) => {
					if (item.name == "specialtyAssort") {
						this.resObj.specialtyAssort1 = item.value
					} else if (item.name == "troubleSource") {
						this.resObj.troubleSource1 = item.value
					} else if (item.name == "attachFunctDept") {
						this.resObj.attachFunctDept1 = item.value
					} else if (item.name == "priorEvaGrade") {
						this.resObj.priorEvaGrade1 = item.value
					} else if (item.name == "evaGrade") {
						this.resObj.evaGrade1 = item.value
					} else if (item.name == "zdGrade") {
						this.resObj.zdGrade1 = item.value
						this.resObj.evaGrade1 = this.resObj.evaGrade1 == "" ? item.value : this.resObj.evaGrade1
					}
				})
			},
			//菜单操作
			clickMenus(path) {
				//如果在跳转名单中，就拼接参数后跳转
				let whiteList = ['auditOpinion', 'transferInformation', 'audit?flag=0', 'audit?flag=1&hidden=1']
				whiteList.forEach(item => {
					if (path == item) {
						if (path.slice(0, 6) == 'audit?') {
							this.request(this.$api.hiddenInfoSave, this.resObj, 'POST').then((res) => {
								item = path + '&item=' + JSON.stringify(this.resObj)
								uni.navigateTo({
									url: './' + item
								})
							})
						} else {
							item = path + '?item=' + JSON.stringify(this.resObj) + '&dictList=' + JSON.stringify(
								this.dictList)
							uni.navigateTo({
								url: './' + item
							})
						}
					}
				})
				//如果在操作名单中，就进行对应操作
				let menuList = ['Inspection', 'Issue', 'applyForAcceptance', 'assignTheAcceptance', 'iconSend',
					'governInfoModification'
				]
				let params = {
					currentState2: this.currentState2,
					// isRnvPeople: 1
				}
				switch (path) {
					case menuList[0]:
						this.request(this.$api.hiddenInfoSave, this.resObj, 'POST').then((res1)=>{
							this.resObj.isRemoveTrouble != 0 ? this.popTitle = '确定验收该隐患信息吗？' : this.popTitle =
								'是否消除隐患为否，隐患未治理完成，确定进入下一轮治理吗？'
							this.popIndex = 0
							this.show = true
						})
						break;
					case menuList[1]:
						this.popTitle = '确定做非隐患处理吗？'
						this.popIndex = 1
						this.show = true
						break;
					case menuList[2]:
						let params1 = {
							currentState2: this.currentState2,
							evaGrade: this.resObj.evaGrade,
							wsflag: this.resObj.wsflag,
							dsflag: this.resObj.dsflag,
						}
						this.request(this.$api.hiddenInfoSave, this.resObj, 'POST').then((res) => {
							this.ysIndex = 0
							this.request(this.$api.getHiddenDangerUserRoleCode, params1, "POST").then(({
								resultValue: res
							}) => {
								this.roleCode = res.roleCode
								this.selfFlag = res.selfFlag
								console.log(this.roleCode)
								this.showfsr = true
							})
						})
						break;
					case menuList[3]:
						this.ysIndex = 1
						let params2 = {
							currentState2: this.currentState2,
							evaGrade: this.resObj.evaGrade,
							wsflag: this.resObj.wsflag,
							dsflag: this.resObj.dsflag,
						}
						this.request(this.$api.getHiddenDangerUserRoleCode, params2, "POST").then(({
							resultValue: res
						}) => {
							this.roleCode = res.roleCode
							this.selfFlag = res.selfFlag
							console.log(this.roleCode)
							this.showfsr = true
						})
						break;
					case menuList[4]:
						this.ysIndex = 2
						console.log(params)
						this.request(this.$api.getHiddenDangerUserRoleCode, params, "POST").then(({
							resultValue: res
						}) => {
							this.roleCode = res.roleCode
							this.selfFlag = res.selfFlag
							console.log(this.roleCode)
							this.showfsr = true
						})
						break;
					case menuList[5]:
						if (this.currentState2 == 'zg') {
							uni.navigateTo({
								url: path + '?item=' + JSON.stringify(this.resObj) + '&dictList=' + JSON.stringify(
									this.dictList)
							})
						} else if (this.currentState2 == 'ys') {
							uni.navigateTo({
								url: path + '?flag1=1&item=' + JSON.stringify(this.resObj) + '&dictList=' + JSON
									.stringify(this.dictList)
							})
						} else {
							uni.navigateTo({
								url: './modifyHiddenDanger?item=' + JSON.stringify(this.resObj) + '&dictList=' +
									JSON.stringify(this.dictList)
							})
						}
						break;
					default:

				}
			},
			//选人弹框的回调函数
			async listItemfs(e) {
				let arr = []
				// console.log(this.resObj, 111111)
				arr.push({
					troubleId: this.resObj.troubleId,
					selectUserId: e.id,
					sendPersonId: e.id,
					currentState2: this.resObj.currentState2,
					evaGrade: this.resObj.evaGrade,
				})
				console.log(arr)
				if (this.ysIndex == 0) {
					// let res = await this.request(this.$api.checkUserPermission, arr, 'POST')
					// console.log(res)
					let res1 = await this.request(this.$api.sendInfo, arr, 'POST')
					uni.showToast({
						icon: "success",
						title: "申请验收成功"
					})
					this.showfsr = false
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				} else if (this.ysIndex == 1) {
					let params = {
						troubleId: this.resObj.troubleId,
						sendPersonId: e.id,
					}
					let res1 = await this.request(this.$api.zhiPaiOtherUser, params, 'POST')
					uni.showToast({
						icon: "success",
						title: "指派验收成功"
					})
					this.showfsr = false
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				} else if (this.ysIndex == 2) {
					// let res = await this.request(this.$api.checkUserPermission, arr, 'POST')
					// console.log(res)
					let res1 = await this.request(this.$api.sendInfo, arr, 'POST')
					uni.showToast({
						icon: "success",
						title: "发送成功"
					})
					this.showfsr = false
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}

			},
			//提交弹框的确定按钮
			async sub() {
				let params = {
					troubleId: this.resObj.troubleId
				}
				if (this.popIndex == 0) {
					if(this.resObj.isRemoveTrouble != 0){
						let res2 = await this.request(this.$api.acceptance, params, 'POST')
						this.show = false
						uni.showToast({
							icon: "success",
							title: res2.resultValue
						})
						setTimeout(item => {
							uni.navigateBack()
						}, 1000)
					}else{//退回
						uni.navigateTo({
							url:'./audit?flag=1&hidden=1&item=' + JSON.stringify(this.resObj)
						})
					}
				} else if (this.popIndex == 1) {
					let res = await this.request(this.$api.nonHiddenInfo, params, 'POST')
					this.show = false
					uni.showToast({
						icon: "success",
						title: "非隐患处理成功！"
					})
					setTimeout(item => {
						uni.navigateBack()
					}, 1000)
				}
			},
		}
	}
</script>

<style scoped>
	.box {
		min-height: 100vh;
		background-color: #F1F2F5;
		box-sizing: border-box;
	}

	.head {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 999;
	}

	.maintenance {
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		height: 228rpx;
		width: 100%;
		box-shadow: 0 -1px 11px 0 rgba(0, 0, 0, 0.09);
		text-align: center;
		padding: 40rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-around;
	}

	.maintenance image {
		width: 84rpx;
		height: 84rpx;
	}

	.maintenance view {
		width: 120rpx;
		font-family: PingFangSC-Regular;
		font-weight: Regular;
		font-size: 28rpx;
		color: #65717D;
	}

	>>>.u-popup__content {
		border-radius: 20rpx;
		overflow: hidden;
	}

	.popup {
		font-size: 32rpx;
		width: 600rpx;
		box-sizing: border-box;
		padding: 60rpx;
	}

	.btn {
		box-sizing: border-box;
		width: 220rpx;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		text-align: center;
		border-radius: 14rpx;
	}

	.but1 {
		border: 1px solid #3473F9;
		color: #3473F9;
	}

	.but2 {
		background-color: #3473F9;
		color: #FFFFFF;
	}
</style>
